package com.q.tqb.util.oss;

import android.os.Handler;

import com.alibaba.sdk.android.oss.ClientException;
import com.alibaba.sdk.android.oss.ServiceException;
import com.alibaba.sdk.android.oss.callback.OSSCompletedCallback;
import com.alibaba.sdk.android.oss.model.PutObjectRequest;
import com.alibaba.sdk.android.oss.model.PutObjectResult;
import com.q.tqb.util.LogUtil;
import com.q.tqb.util.http.IRawResponse;


public class OssCallback implements OSSCompletedCallback<PutObjectRequest, PutObjectResult> {
    private Handler mHandler;
    private IRawResponse mResponse;


    public OssCallback(Handler handler, IRawResponse response) {
        mHandler = handler;
        mResponse = response;
    }

    @Override
    public void onSuccess(final PutObjectRequest request, final PutObjectResult result) {
        mHandler.post(new Runnable() {
            @Override
            public void run() {
//                LogUtil.i("\nObject: " + request.getObjectKey()
//                        + "\nETag: " + result.getETag()
//                        + "\nRequestId: " + result.getRequestId()
//                        + "\nhead: " + result.getResponseHeader().toString()
//                        + "\nCallback: " + result.getServerCallbackReturnBody());

                mResponse.onSuccess(result.getServerCallbackReturnBody());
            }
        });
    }


    @Override
    public void onFailure(PutObjectRequest request, final ClientException clientException, final ServiceException serviceException) {
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                String info = "";
                // 请求异常
                if (clientException != null) {
                    // 本地异常如网络异常等
                    clientException.printStackTrace();
                    info = clientException.toString();
                }
                if (serviceException != null) {
                    // 服务异常
//                    LogUtil.i("ErrorCode" + serviceException.getErrorCode());
//                    LogUtil.i("RequestId" + serviceException.getRequestId());
//                    LogUtil.i("HostId" + serviceException.getHostId());
//                    LogUtil.i("RawMessage" + serviceException.getRawMessage());
                    info = serviceException.toString();
                }
                mResponse.onFailure(info);
            }
        });
    }
}